﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ROrganizer.Client;
using ROrganizer.Client.Files;
using ROrganizer.UI.Controllers;
using ROrganizer.UI.Forms.Core;

namespace ROrganizer.UI.Forms
{
    internal partial class CopySettingsForm : Form
    {
        private readonly CopySettingsFormController controller;
        private readonly GroupManager<CopySettingsFormController.FileEntry, object> grouper;

        public CopySettingsForm(string[] files)
        {
            this.controller = new CopySettingsFormController(files);
            this.InitializeComponent();

            this.grouper = new GroupManager<CopySettingsFormController.FileEntry, object>(this.olvRenamings);
            this.InitializeGroups();

            this.tbDestinationFolder.Text = this.controller.DestinationPath;
            this.cbRenaming.Checked = this.controller.RenameUsingImdb;

            this.olvRenamings.Sort(this.chName, SortOrder.Ascending);
            this.olvRenamings.Objects = this.controller.Files;
            this.controller.Files.CollectionChanged += this.OnChanged;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.olvRenamings.Normalize();
            this.RefreshActions();
        }

        public CopyTask[] GetTasks()
        {
            return this.controller.GetTasks();
        }

        private void InitializeGroups()
        {
            this.grouper.SetFormat<long>(this.chSize, Formatter.FormatSize);
        }

        private void OnChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Reset)
                this.olvRenamings.ClearObjects();

            if (e.OldItems != null)
                this.olvRenamings.RemoveObjects(e.OldItems);

            if (e.NewItems != null)
                this.olvRenamings.AddObjects(e.NewItems);
        }

        private void OnBrowseDestinationFolder(object sender, EventArgs e)
        {
            this.ExecuteAndWatch(this.BrowseDestinationFolder);
        }

        private void BrowseDestinationFolder()
        {
            this.controller.BrowseFolder(this);
            this.tbDestinationFolder.Text = this.controller.DestinationPath;
            this.RefreshActions();
        }

        private void OnShowMoreCopyOptions(object sender, LinkLabelLinkClickedEventArgs e)
        {
        }

        private void OnCommit(object sender, EventArgs e)
        {
            this.ExecuteAndWatch(this.Commit);
        }

        private void Commit()
        {
            this.controller.Commit();
            this.DialogResult = DialogResult.OK;
        }

        private void OnRenameUsingImdb(object sender, EventArgs e)
        {
            this.ExecuteAndWatch(this.RenameUsingImdb);
        }

        private void RenameUsingImdb()
        {
            this.controller.RenameUsingImdb = this.cbRenaming.Checked;
            this.controller.Refresh();
            this.RefreshActions();
        }

        private void RefreshActions()
        {
            this.btnOK.Enabled = this.controller.CanCommit();
        }

        private void OnMouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.ExecuteAndWatch(this.controller.PlayMovie, this.olvRenamings.SelectedObject as CopySettingsFormController.FileEntry);
        }
    }
}
